package org.yzh.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

import java.io.*;


/**
 * hdfs涉及的所有方法
 * 
 * @author 云计算应用与开发项目组
 * @since V2.0
 * 
 */
public class HdfsClient {

	static FileSystem fs;
	static Configuration conf;
	private static final HdfsClient instance = new HdfsClient();

	public static HdfsClient getInstance() {
		return instance;
	}

	private HdfsClient() {
		conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://192.168.137.250:8020");
		try {
			fs = FileSystem.get(conf);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 上传文件
	 * 
	 * @param localfile
	 *                :本地的文件路径
	 * @param remotefile
	 *                ：上传到hdfs上的文件路径
	 * @throws Exception
	 */
	public void upload(String localfile, String remotefile) {
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(localfile));
			OutputStream out = fs.create(new Path(remotefile));
			IOUtils.copyBytes(in, out, 4096, true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 下载文件
	 * 
	 * @param path
	 * @param local
	 * @throws Exception
	 */
	public void downLoad(String path, String local) throws Exception {
		FSDataInputStream in = fs.open(new Path(path));
		OutputStream out = new FileOutputStream(local);
		IOUtils.copyBytes(in, out, 4096, true);
	}

	/**
	 * 删除文件及文件夹
	 * 
	 * @param name
	 * @throws Exception
	 */
	public void delete(String name) throws Exception {
		fs.delete(new Path(name), true);
	}

	public static void main(String[] args) throws Exception {
		
		File file2 = new File(System.getProperty("user.dir") + "/webmagic/wea.txt");
		HdfsClient hdfsDB = new HdfsClient();
		hdfsDB.upload(file2.getAbsolutePath(), "/tmp/" + file2.getName());
//		HdfsClient hdfsDB = new HdfsClient();
//		hdfsDB.delete("/user/51job/output/seqinput");
//		for (int i = 1; i < 31; i ++) {
//		File file2 = new File(System.getProperty("user.dir") + "/webmagic/input/"+i+".txt");
//		hdfsDB.upload(file2.getAbsolutePath(), "/user/51job/input/" + file2.getName());
//		}
	}
}